from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
from datetime import  timedelta
import random
import Conn
import pymysql

def tick():
    print (datetime.now())

def tick2(num,atime,kthe):
   sql = Conn.Conn.sql2
   mysqlconn = Conn.Conn.conn
   cursor = mysqlconn.cursor()
   query = (num,atime,kthe)
   cursor.execute(sql % query)
   mysqlconn.commit()
   print("do")

if __name__ == "__main__":
    sche = BlockingScheduler()
    for line in open("d:\\task.txt","rt"):
        line = line.replace('\n',"")
        if(line==''):
            break
        aa = line.split('|')
        rint = random.randint(2,10)
        t_time = datetime.strptime(aa[1].strip(),'%Y-%m-%d %H:%M:%S')-timedelta(seconds=20)
        atime = datetime.strptime(aa[1].strip(),'%Y-%m-%d %H:%M:%S')-timedelta(seconds=rint)
        sche.add_job(tick2,'date',run_date=t_time.strftime( '%Y-%m-%d %H:%M:%S'),args=[aa[2],atime,aa[0]])
    sche.start()